﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Data;

namespace Ylh.Data
{
    public partial class EntityBase
    {
        #region 和属性相关
        public EntityBase() { }
        /// <summary>
        /// 重置变化的属性
        /// </summary>
        public void Reset()
        {
            for (int i = 0; i < Changlist.Length; i++)
            {
                Changlist[i] = false;
            }
        }
        /// <summary>
        /// 获取属性改变的列
        /// </summary>
        public List<String> PropertyChangeList
        {
            get
            {
                List<String> _propertyChangeList = new List<string>();
                for (int i = 0; i < PropertyNames.Length; i++)
                {
                    if (Changlist[i])
                    {
                        _propertyChangeList.Add(PropertyNames[i]);
                    }
                }
                return _propertyChangeList;
            }
        }
        private bool[] Changlist;
        private String[] _propertyNames;
        /// <summary>
        /// 用于装载对象中的所有属性名
        /// </summary>
        protected String[] PropertyNames
        {
            get
            {
                if (_propertyNames == null)
                {
                    this.SetFieldNames();
                    Changlist = new bool[PropertyNames.Length];
                }
                return _propertyNames;
            }
            set { _propertyNames = value; }
        }
        private object[] _propertyValues;
        /// <summary>
        /// 装载对象中的所有属性的值
        /// </summary>
        protected object[] PropertyValues
        {
            get
            {
                if (_propertyValues == null)
                {
                    _propertyValues = new object[PropertyNames.Length];
                }
                return _propertyValues;
            }
            set { _propertyValues = value; }
        }
        /// <summary>
        /// 虚方法
        /// 在model中重写
        /// </summary>
        protected virtual void SetFieldNames() { }
        /// <summary>
        /// 设置属性的值
        /// </summary>
        /// <param name="Name">属性名称</param>
        /// <param name="Value">属性对应的值</param>
        protected void SetProperty(String Name, object Value)
        {
            for (int i = 0; i < PropertyNames.Length; i++)
            {
                if (PropertyNames[i].ToLower() == Name.ToLower())
                {
                    //这里添加值变化事件 
                    Changlist[i] = true;
                    PropertyValues[i] = Value;
                    break;
                }
            }
        }
        private T ConvertType<T>(object obj)
        {
            if (obj != null)
                return (T)obj;
            else if (obj == DBNull.Value || obj == null)
            {
                if (typeof(T) == typeof(DateTime))
                {
                    object o = DateTime.MinValue;
                    return (T)o;
                }
                else
                    return default(T);
            }
            else
            {
                //如果 Value为 decimal类型，Ｔ　为double 类型， (T)Value 将发生错误
                return (T)Convert.ChangeType(obj, typeof(T));
            }
        }
        protected object GetPropertyValue(String Name)
        {
            for (int i = 0; i < PropertyNames.Length; i++)
            {
                if (String.Compare(PropertyNames[i], Name, true) == 0)
                {
                    return PropertyValues[i];
                }
            }
            return null;
        }
        /// <summary>
        /// 获取属性的值
        /// </summary>
        /// <typeparam name="T">属性类型</typeparam>
        /// <param name="Name">属性名称</param>
        /// <returns>对应属性类型的值</returns>
        protected T GetProperty<T>(String Name)
        {
            //这里添加属性变化事件
            OnPropertyNameGetting(Name);
            object obj = GetPropertyValue(Name);
            return ConvertType<T>(obj);
        }

        public event EventHandler<PropertyNameGettingEventArgs> PropertyNameGetting;
        public virtual void OnPropertyNameGetting(String Name)
        {
            if (this.PropertyNameGetting != null)
            {
                this.PropertyNameGetting(this, new PropertyNameGettingEventArgs(Name));
            }
        }
        private String _tableName;
        public String TableName
        {
            get { return _tableName; }
            set { _tableName = value; }
        }
        private Db_DataType _entityMap = Db_DataType.Table;
        /// <summary>
        /// 实体类的映射类型
        /// </summary>
        protected internal Db_DataType EntityMap
        {
            get { return _entityMap; }
            set { _entityMap = value; }
        }
        #endregion

        #region 插入通用方法
        protected static bool InserObjedct(Type T, Object O, List<String> IExcept, String ConStr)
        {
            return RefInSert.InserObjedct(T, O, IExcept, ConStr);
        }

        protected static bool InsertDatatable(Type T, DataTable dt, String ConStr)
        {
            return RefInSert.InsertDatatable(T, dt, ConStr);
        }

        #endregion

        #region 查询通用方法
        protected static DataTable Select(Type T, SqlWhere where, String ConStr)
        {
            return RefSelect.Select(T, where, ConStr);
        }

        protected static DataTable Select(Type T, OQLWhere where, string ConStr)
        {
            return RefSelect.Select(T, where, ConStr);
        }

        protected static DataTable Select(Type T, QueryPage page, string ConStr)
        {
            return RefSelect.Select(T, page, ConStr);
        }

        protected static DataTable Select(Type T, OQLWhere where, String SelectColumn, String ConStr)
        {
            return RefSelect.Select(T, where, SelectColumn, ConStr);
        }
        #endregion

        #region 删除通用方法
        protected static int Delete(Type T, OQLWhere where, String ConStr)
        {
            return RefDel.Delete(T, where, ConStr);
        }

        #endregion

        protected static int Update(Type T, object O, string ConStr)
        {
            return RefUpdate.Update(T, O, ConStr);
        }


        protected static int Update(Type T, DataTable dt, string ConStr)
        {
            return RefUpdate.Update(T, dt, ConStr);
        }





    }
}
